﻿#region Imports
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using CommonUtilities.Commands;
using CommonUtilities.Extensions;
using PlayRandom.Models;
using PlayRandom.ViewModels;
#endregion Imports

namespace PlayRandom.Views
{
    public partial class MainWindow : Window
    {
        #region Constructors
        public MainWindow()
        {
            InitializeComponent();
        }
        #endregion Constructors

        #region Events
        private void mainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                var vm = new MainViewModel();
                this.DataContext = vm;

                // this.txtSearch.WatermarkText = vm.WatermarkText; RKD
                vm.ScrollToViewCommand = new RelayCommand<FileDetails>((fd) => { lstFavFiles.ScrollIntoView(fd); });
                vm.DirPathFocusCommand = new RelayCommand((parameter) => { txtDirPath.Focus(); txtDirPath.SelectAll(); });
                vm.SearchFavFocusCommand = new RelayCommand((parameter) => { txtSearch.Focus(); txtSearch.SelectAll(); });
            }));
        }

        private void SelectedRandom_MouseEnter(object sender, MouseEventArgs e)
        {
            (sender as Button).Foreground = Brushes.Black;
        }

        private void SelectedRandom_MouseLeave(object sender, MouseEventArgs e)
        {
            (sender as Button).Foreground = Brushes.White;
        }

        private void mainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                    var vm = this.DataContext as MainViewModel;
                    if (txtSearch.IsFocused && !vm.FileSearchText.IsNullOrEmpty())
                        vm.FileSearchText = String.Empty;
                    else
                    {
                        if (MessageBox.Show("Exit application?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            Application.Current.Shutdown();
                        else e.Handled = true;
                    }
                    break;

                default: break;
            }
        }
        #endregion Events
    }
}
